package View;

import java.util.ArrayList;
import java.util.Scanner;

import Controllers.DeskController;
import Models.CheckedOutException;
import Models.CredCheckError;
import Models.Equipment;

public class Prompt {

	public static void start() {
		System.out.println("Welcome to the Equipment Management System!");
		System.out.println("Type \"help\" for command options.");
		System.out.println();
		ask();
		System.out.println("Thank You!");
		return;
	}

	private static void ask() {
		Scanner in = new Scanner(System.in);
		DeskController d = new DeskController();

		boolean b = true;
		while (b) {
			System.out.print(">");
			in = new Scanner(System.in);
			String next = in.nextLine();

			if (next.equalsIgnoreCase("help"))
				help();
			else if (next.equalsIgnoreCase("exit"))
				b = false;
			else if (next.equalsIgnoreCase("add user"))
				addUser(in, d);
			else if (next.equalsIgnoreCase("add equipment"))
				addEquipment(in, d);
			else if (next.equalsIgnoreCase("remove user"))
				removeUser(in, d);
			else if (next.equalsIgnoreCase("remove equipment"))
				removeEquipment(in, d);
			else if (next.equalsIgnoreCase("search"))
				search(in, d);
			else if (next.equalsIgnoreCase("overdue equipment"))
				overdueEquipment(in, d);
			else if (next.equalsIgnoreCase("select equipment"))
				selectEquipment(in, d);
			else if (next.equalsIgnoreCase("check out"))
				checkOut(in, d);
			else if (next.equalsIgnoreCase("check in"))
				checkIn(in, d);
			else if (next.equalsIgnoreCase("extend checkout"))
				extendCheckOut(in, d);
			else if (next.equalsIgnoreCase("user history"))
				userHistory(in, d);
			else
				invalid();
		}
	}

	private static void help() {
		System.out
				.println("exit: \t\t\t Exit the Equipment Management System program");
		System.out
				.println("add user: \t\t Add a user to the list of registered users");
		System.out
				.println("remove user: \t\t Remove a user from the list of registered users");
		System.out
				.println("add equipment: \t\t Add a piece of equipment to the list of registered equipment");
		System.out
				.println("remove equipment: \t Remove a piece of equipment from the list of registered equipment");
		System.out
				.println("search: \t\t Search for a piece of equipment and display its information");
		System.out
				.println("overdue equipment: \t Display a list of overdue equipment");
		System.out
				.println("select equipment: \t Display the information of a specific equipment");
		System.out
				.println("check out: \t\t Check out a piece of equipment for someone");
		System.out
				.println("check in: \t\t Check in a piece of equipment that has been checked out");
		System.out.println("extend checkout: \t Renew an item");
		System.out.println("user history: \t\t Get a user's history");
	}

	private static void addUser(Scanner in, DeskController d) {
		String netID, first, last, workerID;
		int room;
		boolean ra = false;

		System.out.print("student netID: ");
		netID = in.next();
		System.out.print("room number: ");
		room = in.nextInt();
		System.out.print("first name: ");
		first = in.next();
		System.out.print("last name: ");
		last = in.next();
		System.out.print("isRA (true or false): ");
		String temp = in.next();
		if (temp.equalsIgnoreCase("true"))
			ra = true;
		else if (temp.equalsIgnoreCase("false"))
			ra = false;
		else {
			invalid();
			return;
		}
		System.out.print("Enter your ID:");
		workerID = in.next();
		try {
			d.addUser(netID, room, first, last, ra, workerID);
		} catch (CredCheckError e) {
			permission();
		}
	}

	private static void userHistory(Scanner in, DeskController d) {
		try {
			System.out.print("user netID: ");
			String next = in.next();
			ArrayList<Integer> list = d.userHistory(next);
			for (int i = 0; i < list.size(); i++) {
				Equipment e = d.selectEquipment(list.get(i));
				System.out.println("Name: " + e.getName() + "   ID: "
						+ e.getID());
			}
		} catch (NullPointerException r) {
			System.out.println("User does not exist.");
		}
	}

	private static void extendCheckOut(Scanner in, DeskController d) {
		System.out.print("Equipment ID: ");
		int id = in.nextInt();
		System.out.print("Enter your ID: ");
		String workerID = in.next();
		try {
			d.extendCheckOut(workerID, id);
		} catch (CredCheckError e) {
			permission();
		}

	}

	private static void checkOut(Scanner in, DeskController d) {
		System.out.print("netID: ");
		String netID = in.next();
		System.out.print("Equipment ID: ");
		int eID = in.nextInt();
		System.out.print("Enter your ID: ");
		String w = in.next();
		try {
			d.checkOut(netID, w, eID);
		} catch (CredCheckError e) {
			permission();
		} catch (CheckedOutException e) {
			System.out.println("Already checked out.");
		}
	}

	private static void checkIn(Scanner in, DeskController d) {
		System.out.print("Equipment ID: ");
		int id = in.nextInt();
		System.out.print("Enter your ID: ");
		String workerID = in.next();
		try {
			d.checkIn(workerID, id);
		} catch (CredCheckError e) {
			permission();
		}
	}

	private static void selectEquipment(Scanner in, DeskController d) {
		System.out.print("Equipment ID: ");
		int next = in.nextInt();
		Equipment e = d.selectEquipment(next);
		displayInfo(e);
	}

	private static void overdueEquipment(Scanner in, DeskController d) {
		ArrayList<Equipment> equipment = d.overdueEquipment();
		for (int i = 0; i < equipment.size(); i++) {
			displayInfo(equipment.get(i));
			System.out.println();
		}
	}

	private static void search(Scanner in, DeskController d) {
		System.out.println("search: ");
		String query = in.next();
		ArrayList<Equipment> equip = d.search(query);
		for (int i = 0; i < equip.size(); i++) {
			displayInfo(equip.get(i));
			System.out.println();
		}
	}

	private static void removeEquipment(Scanner in, DeskController d) {
		System.out.print("Equipment ID: ");
		int id = in.nextInt();
		System.out.print("Enter your ID: ");
		String workerID = in.next();
		try {
			d.removeEquipment(id, workerID);
		} catch (CredCheckError e) {
			permission();
		}
	}

	private static void removeUser(Scanner in, DeskController d) {
		System.out.print("netID: ");
		String id = in.next();
		System.out.print("Enter your ID: ");
		String workerID = in.next();
		boolean b = d.removeUser(id, workerID);
		if (!b)
			permission();
	}

	private static void addEquipment(Scanner in, DeskController d) {
		System.out.print("name: ");
		String name = in.next();
		System.out.print("Number of checkout days: ");
		int num = in.nextInt();
		System.out.print("Enter your ID: ");
		String workerID = in.next();
		try {
			d.addEquipment(name, num, workerID);
		} catch (CredCheckError e) {
			permission();
		}
	}

	private static void invalid() {
		System.out.println();
		System.out
				.println("invalid command- type \"help\" for command options");
	}

	private static void permission() {
		System.out.println("Only workers may use this command.");
	}

	private static void displayInfo(Equipment e) {
		try {
			System.out.println("\tID: " + e.getID());
			System.out.println("\tName: " + e.getName());
			if (e.isCheckedOut()) {
				System.out
						.println("\tCurrent User: " + e.getCurrentUserNetID());
				if (e.isOverdue()) {
					System.out.println("\tOverdue.");
				} else {
					long elapsed = System.currentTimeMillis()
							- e.getTimeCheckedOut();
					int days = (int) elapsed / 86400000;
					System.out.println("\t" + (e.getCheckOutDays() - days)
							+ " days left");
				}
			}
		} catch (NullPointerException f) {
			System.out.println("No such equipment.");
		}
	}

}
